<?php
/**
 * Created by PhpStorm.
 * @author WYZ <wyz@jungo.com.cn>
 * @copyright 深圳市俊网网络有限公司
 */

namespace Selibra\Model;


use Selibra\Config\Config;

class EntityProxy
{
    /**
     * @var object
     */
    protected object $entity;

    public function __construct(object $entity)
    {
        $this->entity = $entity;
    }


    /**
     * @param $name
     * @param $value
     * @throws \ReflectionException
     */
    public function setProperty($name, $value)
    {
        // 检测该类型是否是数组
//        if (is_string($value)) {
//            $preValue = json_decode($value, true);
//            if (json_last_error() === JSON_ERROR_NONE) {
//                $reflection = new \ReflectionObject($this->entity);
//                $property = $reflection->getProperty($name);
//                if( $name === 'images' ) {
//                    Console::log($property->getType());
//                }
//                if ($property->getType() === 'array') {
//                    $value = $preValue;
//                }
//            }
//        }
        $this->entity->$name = $value;
    }


    /**
     * @return array
     */
    public function selectProperty()
    {
        return get_class_vars(get_class($this->entity));
    }


    /**
     * @return array
     */
    public function toArrayData()
    {
        $property = $this->selectProperty();
        $data = [];
        foreach ($property as $key => $var) {
            $method = 'get' . ucfirst($key);
            if (!method_exists($this->entity, $method)) {
                $method = 'get' . self::toCamelCase(ucfirst($key));
            }
            if (method_exists($this->entity, $method)) {
                $data[$key] = $this->entity->$method();
            } else {
                if (isset($this->entity->$key)) {
                    $data[$key] = $this->entity->$key;
                }
            }
            if ($this->entity->$key === Config::get('application.db.null_substitutes')) {
                $data[$key] = null;
            }
        }
        return $data;
    }


    /**
     * @return array
     */
    public function toPropertyArrayData()
    {
        $property = $this->selectProperty();
        $data = [];
        foreach ($property as $key => $var) {
            if (isset($this->entity->$key)) {
                if (is_array($this->entity->$key)) {
                    $this->entity->$key = json_encode($this->entity->$key);
                }
                $data[$key] = $this->entity->$key;
            }
        }
        return $data;
    }


    /**
     * @return object|mixed
     */
    public function getEntity(): object
    {
        return $this->entity;
    }


    /**
     * @param object $entity
     * @return array
     */
    public static function entityConversionToArray(object $entity)
    {
        return (new self($entity))->toArrayData();
    }


    /**
     * 数组转换为实体
     */
    public function writeData(array $array): void
    {
        $this->selectProperty();
        $data = [];
        foreach ($array as $key => $var) {
            $method = 'set' . ucfirst($key);
            if (!method_exists($this->entity, $method)) {
                $method = 'set' . self::toCamelCase(ucfirst($key));
            }
            if (method_exists($this->entity, $method)) {
                $this->entity->$method($var);
            } else {
                if (isset($this->entity->$key)) {
                    $this->entity->$key = $var;
                }
            }
        }
    }


    /**
     * 下划线转大小写
     * @param string $method
     * @return mixed|string
     */
    private static function toCamelCase($method)
    {
        $array = explode('_', $method);
        $result = $array[0];
        $len = count($array);
        if ($len > 1) {
            for ($i = 1; $i < $len; $i++) {
                $result .= ucfirst($array[$i]);
            }
        }
        return $result;
    }


}
